package com.chalk.service.impl;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.chalk.dao.VenueDao;
import com.chalk.model.Venue;
import com.chalk.service.CollectionService;
import com.chalk.service.VenueAndSeService;
import com.chalk.service.VenueService;

import com.chalk.util.MapUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author glq
 * @since 2020-12-10
 */

@Service
public class VenueServiceImpl extends ServiceImpl<VenueDao, Venue> implements VenueService {

    @Autowired
    VenueAndSeService venueAndSeService;
    @Autowired
    CollectionService collectionService;

    @Override
    public int addVenue(Venue entity) {
        return baseMapper.insert(entity);
    }

    @Override
    public int updateVenue(Venue entity) {
        return baseMapper.updateById(entity);
    }


    @Override
    public int deleteVenue(Integer id) {
        return baseMapper.deleteById(id);
    }

    @Override
    public List<Venue> getVenuesToList(Integer isRecommend) {
        //倒序字段
        List<String> desc = new ArrayList<>();
        desc.add("create_time");
        //条件构造器
        EntityWrapper<Venue> wrapper = new EntityWrapper<>();
        wrapper.eq("is_recommend", isRecommend);
        wrapper.eq("is_delete", 0);
        wrapper.orderDesc(desc);
        return selectList(wrapper);
    }

    @Override
    public Page<Venue> getVenuesForPage(Map<String, Object> map) {
        String seId = (String) map.get("seId");
        String venueName = (String) map.get("venueName");
        //排序字段
        List<String> desc = new ArrayList<>();
        desc.add("create_time");
        //条件构造
        EntityWrapper<Venue> wrapper = new EntityWrapper<>();
        if (StringUtils.isNotBlank(seId)) {
            List<Integer> list = venueAndSeService.getVenuesBySeId(Integer.valueOf(seId));
            if (list.size() == 0) {
                return new Page<Venue>(1, 10);
            }
            wrapper.in("venue_id", list);
        }
        if (StringUtils.isNotBlank(venueName)) {
            wrapper.like("venue_name", venueName);
        }
        wrapper.eq("is_delete", 0);
        Page<Venue> page = new Page<>(MapUtil.getPage(map), MapUtil.getSize(map));
        page.setDescs(desc);
        Page<Venue> venuePage = selectPage(page, wrapper);
        return venuePage;
    }

    @Override
    public Boolean isVenueExist(Integer venueId) {
        //构造器
        EntityWrapper<Venue> wrapper = new EntityWrapper<>();
        wrapper.eq("venue_id", venueId);
        wrapper.eq("is_delete", 0);
        Venue venue = selectOne(wrapper);
        if (venue == null) {
            return false;
        }
        return true;
    }

    @Override
    public List<Venue> getAllVenues(Map<String,Object> map) {
       String seId=(String)map.get("seId");
        //排序字段
        List<String> desc=new ArrayList<>();
        desc.add("create_time");
        //构造器
        EntityWrapper<Venue> wrapper = new EntityWrapper<>();
        wrapper.eq("is_delete", 0);
        wrapper.orderDesc(desc);
        if(StringUtils.isNotBlank(seId)){
            wrapper.in("venue_id",venueAndSeService.getVenuesBySeId(Integer.valueOf(seId)));
        }

        return selectList(wrapper);
    }

    @Override
    public Page<Venue> getCollectionVenues(Map<String, Object> map) {
        String memberId=(String)map.get("memberId");
        //排序字段
        List<String> desc=new ArrayList<>();
        desc.add("create_time");
        //构造器
        EntityWrapper<Venue> wrapper = new EntityWrapper<>();
        wrapper.eq("is_delete", 0);

        Page<Venue> page=new Page<>(MapUtil.getPage(map),MapUtil.getSize(map));
        List<Integer> list=collectionService.getVenueIdsByMemberId(memberId);
        if(list.size()==0){
            return page;
        }else {
           wrapper.in("venue_id",list);
           return selectPage(page,wrapper);
        }
    }
}
